package com.warrantyhotel.android.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.util.Log;

import com.warrantyhotel.android.model.Keys;

public class DateUtis {

	public static String parseDate(String date) {
		String dateValue = date.substring("/Date(".length(), date.indexOf("+"));
		long time = Long.valueOf(dateValue);
		String sTz = date.substring(date.indexOf("+"), date.indexOf(")"));
		final TimeZone tz = TimeZone.getTimeZone("GMT+" + sTz);
		final Calendar cal = Calendar.getInstance(tz);
		cal.setTimeInMillis(time);
		final SimpleDateFormat f = new SimpleDateFormat(Keys.dateFormat);
		f.setTimeZone(tz);
		return f.format(cal.getTime());
	}

	public static String parseDateFromMilliseconds(Long time) {
		final SimpleDateFormat f = new SimpleDateFormat(Keys.dateFormat);
		return f.format(time);
	}

	public static long parseDateToMillisecinds(String date) {
		long result = 0l;
		try {
			final long time = Long.valueOf(date.substring("/Date(".length(), date.indexOf("+")));
			String sTz = date.substring(date.indexOf("+"), date.indexOf(")"));
			final TimeZone tz = TimeZone.getTimeZone("GMT+" + sTz);
			final Calendar cal = Calendar.getInstance(tz);
			cal.setTimeInMillis(time);
			result = cal.getTimeInMillis();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String decodeDate(long date) {
		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
		// long epoch = System.currentTimeMillis();
		String result = "/Date(" + date + Keys.timeZoneDateFormat.format(calendar.getTime()) + ")/";
		Log.e("result", result);
		return result;
	}

	public static String decodeDateInbox(long date) {

		
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
		f.setTimeZone(TimeZone.getTimeZone("UTC"));
//		System.out.println(f.format(new Date()));
		String utcTime = f.format(new Date());
		try {
			date = f.parse(utcTime).getTime()/1000;
//			System.out.println("vvvvvvv " + utcTime + " " + f.parse(utcTime).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		String result = "/Date(" + date + ")/";
		Log.e("result", result);
		
//		Calendar calendar = Calendar.getInstance(Locale.getDefault());
//		calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
//		long epoch = System.currentTimeMillis() / 1000;
//		String result = "/Date(" + epoch + Keys.timeZoneDateFormat.format(calendar.getTime()) + ")/";
//		Log.e("result", result);
		return result;
	}

}
